package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.UB;
import com.ykt.converter.MarketConverter;
import com.ykt.converter.MediaSourceConverter;
import com.ykt.model.Market;
import com.ykt.model.MediaSource;
import com.ykt.service.MarketService;
import com.ykt.service.MediaSourceService;
import com.ykt.validator.CommonValidator;
import com.ykt.vo.MarketVo;
import com.ykt.vo.MediaSourceVo;
import com.ykt.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 市场
 *
 * @author wing
 * @version 1.0.0
 */
public class MarketController extends BaseController {
    private final static Logger LOGGER = LoggerFactory.getLogger(MarketController.class);


    private MarketService marketService = enhance(MarketService.class.getName(), MarketService.class);
    private MediaSourceService mediaSourceService = enhance(MediaSourceService.class.getName(), MediaSourceService.class);


    @Before(CommonValidator.LonLatValidator.class)
    public void detail() {
        int userId = getParaToInt("userId", -1);
        double longitude = UB.d(getPara("longitude"), 0);
        double latitude = UB.d(getPara("latitude"), 0);


        Market market = marketService.findByDistance(longitude, latitude);
        if(market == null || market.get(Market.id) == null){
            LOGGER.info("查询市场详情为空, longitude: {}, latitude: {}", longitude, latitude);
            this.returnJson(ResultVo.success(market));
            return ;
        }

        MarketVo marketVo = MarketConverter.convert(market);

        this.setMediaSources(Arrays.asList(marketVo));

        this.returnJson(ResultVo.success(marketVo));
    }

    /**查询所有市场，并按距离由近道远排列*/
    public void findAll() {
        Integer userId = getParaToInt(ParamNames.userId, -1);
        double longitude = UB.d(getPara("longitude"), 0);
        double latitude = UB.d(getPara("latitude"), 0);

        List<Market> markets = marketService.findAll(longitude, latitude);
        List<MarketVo>  vos = MarketConverter.convert(markets);

        this.setMediaSources(vos);

        this.returnJson(ResultVo.success(vos));
    }


    private void setMediaSources(List<MarketVo> vos){
        List<Integer> bizIds = new ArrayList<>();
        for (MarketVo vo : vos) {
            bizIds.add(vo.getId());
        }

        Map<String, List<MediaSource>> mediaSourceMap = mediaSourceService.find(MediaSource.BizType.MARKET, bizIds);
        if(mediaSourceMap == null){
            return ;
        }

        for (MarketVo marketVo : vos) {

            String key =MediaSource.BizType.MARKET.name() + marketVo.getId();
            List<MediaSource> mediaSources = mediaSourceMap.get(key);

            List<MediaSourceVo> mediaSourceVos = MediaSourceConverter.convert(mediaSources);
            marketVo.setMediaSources(mediaSourceVos);
        }
    }

}
